package com.qijian.common.utils;

import com.aspose.cad.Color;
import com.aspose.cad.Image;
import com.aspose.cad.imageoptions.CadRasterizationOptions;
import com.aspose.cad.imageoptions.PdfOptions;
import com.aspose.cad.imageoptions.PngOptions;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * CAD转化
 */
public class CADFileUtil {
    /**
     * CAD转PNG
     *
     * @param sourcePath /
     */
    public static void CADFileToImage(String sourcePath, String targerPath, String fileName) {
        CadRasterizationOptions cadRasterizationOptions = new CadRasterizationOptions();
        cadRasterizationOptions.setBackgroundColor(Color.getWhite());
        cadRasterizationOptions.setPageWidth(1600);
        cadRasterizationOptions.setPageHeight(1600);
        PngOptions pngOptions = new PngOptions();
        pngOptions.setVectorRasterizationOptions(cadRasterizationOptions);
        String[] str = sourcePath.split("\\.");
        Image objImage = Image.load(sourcePath);
        if (!fileName.contains(".png"))
            fileName = fileName + ".png";
        objImage.save(targerPath + "/" + fileName, pngOptions);
    }

    /**
     * 方法描述  CAD文件转换为PDF
     *
     * @param sourcePath 选择CAD文件路径
     */
    public static void CADFileToPDF(String sourcePath, String targerPath, String fileName) {
        CadRasterizationOptions cadRasterizationOptions = new CadRasterizationOptions();
        cadRasterizationOptions.setBackgroundColor(Color.getWhite());
        cadRasterizationOptions.setPageWidth(1600);
        cadRasterizationOptions.setPageHeight(1600);
        PdfOptions pdfOptions = new PdfOptions();
        pdfOptions.setVectorRasterizationOptions(cadRasterizationOptions);
        String[] str = sourcePath.split("\\.");
        Image image = Image.load(sourcePath);
        image.save(targerPath + "/" + fileName + ".pdf", pdfOptions);
    }

    /**
     * CAD流转图片流
     *
     * @param in /
     * @return /
     * @throws Exception /
     */
    public static InputStream CADFileToImage(InputStream in) throws Exception {
        Image image = Image.load(in);
        CadRasterizationOptions cadRasterizationOptions = new CadRasterizationOptions();
        cadRasterizationOptions.setBackgroundColor(Color.getWhite());
        cadRasterizationOptions.setPageWidth(1600);
        cadRasterizationOptions.setPageHeight(1600);
        PngOptions pngOptions = new PngOptions();
        pngOptions.setVectorRasterizationOptions(cadRasterizationOptions);
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        image.save(out, pngOptions);
        return outputStreamConvertInputStream(out);
    }

    /**
     * 方法描述  CAD文件转换为PDF
     *
     * @param in 选择CAD文件流
     */
    public static InputStream CADFileToPDF(InputStream in) {
        Image image = Image.load(in);
        CadRasterizationOptions cadRasterizationOptions = new CadRasterizationOptions();
        cadRasterizationOptions.setBackgroundColor(Color.getWhite());
        cadRasterizationOptions.setPageWidth(1600);
        cadRasterizationOptions.setPageHeight(1600);
        PdfOptions pdfOptions = new PdfOptions();
        pdfOptions.setVectorRasterizationOptions(cadRasterizationOptions);
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        image.save(out, pdfOptions);
        return outputStreamConvertInputStream(out);
    }

    /**
     * 方法描述 outputStream转inputStream
     *
     * @param out /
     */
    public static ByteArrayInputStream outputStreamConvertInputStream(final OutputStream out) {
        ByteArrayOutputStream byteOut = (ByteArrayOutputStream) out;
        return new ByteArrayInputStream(byteOut.toByteArray());
    }
}
